package com.guguguweb.rpc.register.imp;

import com.alibaba.nacos.api.exception.NacosException;
import com.guguguweb.rpc.enumeration.RpcError;
import com.guguguweb.rpc.exception.RpcException;
import com.guguguweb.rpc.register.ServiceRegistry;
import com.guguguweb.rpc.util.NacosUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetSocketAddress;

/**
 * @description Nacos服务注册中心
 */
public class NacosServiceRegistry implements ServiceRegistry {

    private static final Logger log = LoggerFactory.getLogger(NacosServiceRegistry.class);


    private NacosUtil nacosUtil;
    public NacosServiceRegistry(NacosUtil nacosUtil){
        this.nacosUtil = nacosUtil;
    }
    /**
     * @description 将服务的名称和地址注册进服务注册中心
     * @param inetSocketAddress
     * param serviceName
     * @return [void]
     *
     */
    @Override
    public void register(String serviceName, InetSocketAddress inetSocketAddress) {
        try {
            //向Nacos注册服务
            nacosUtil.registerService(serviceName, inetSocketAddress);
        }catch (NacosException e) {
            log.error("注册服务时有错误发生" + e);
            throw new RpcException(RpcError.REGISTER_SERVICE_FAILED);
        }
    }

}